#include "SRCommand.h"
#include "global.h"
#include <QDebug>
#include "SRCommandArgumentInterface.h"
#include "SRCommandNamedArgument.h"


// ----- SET VALUES FOR STATIC VARIABLES -----
QChar SRCommand::commandSeparator = QChar('#');
QChar SRCommand::commandNameSeparator = QChar(123); //
QChar SRCommand::commandArgumentSeparator = QChar(124); //
QChar SRCommand::commandArgumentNameSeparator = QChar(125);
QChar SRCommand::commandArgumentValueSeparator = QChar(126); //
QChar SRCommand::packageStopByte = QChar(96);




SRCommand::SRCommand()
{

}


SRCommand::SRCommand(QString c)
{
	SRCommand();


	// encapsulate
	commandName = c.split(SRCommand::commandArgumentSeparator).first();
	QStringList argStringList = c.split(SRCommand::commandArgumentSeparator);

	// iterate over the arguments in the string and append them to the list of arguments
	for(int i=0; i<argStringList.size(); i++)
	{
		QString currentArgString = argStringList.at(i); // as string
		QStringList currentArgStringList = currentArgString.split(SRCommand::commandArgumentValueSeparator); // as string list

		QString argName = currentArgStringList.at(0); // the argument name
		currentArgStringList.removeFirst(); // now there's only values left in the string list
		commandArguments.append(new SRCommandNamedArgument(argName, currentArgStringList));
	}
}


SRCommand::~SRCommand()
{
	// TODO
}


QString SRCommand::toString()
{
	QString stringRepresentation; // build this string
	stringRepresentation = commandName;

	if(!commandArguments.isEmpty()) // not empty
	{
		stringRepresentation += SRCommand::commandNameSeparator;

		for(int i=0; i<commandArguments.size(); i++)
		{
			SRCommandArgumentInterface *currentArg = commandArguments.at(i);
			stringRepresentation += currentArg->toString();
		}
	}

	// end of string. append the package stop byte
	stringRepresentation += SRCommand::packageStopByte;


	return stringRepresentation;
}


QString SRCommand::getName()
{
	return commandName;
}


QList<SRCommandArgumentInterface*> SRCommand::getArguments()
{
	return commandArguments;
}

SRCommandArgumentInterface* SRCommand::getArgument(int i)
{
	return commandArguments.at(i);
}


void SRCommand::setName(QString name)
{
	commandName = name;
}


int SRCommand::argumentCount()
{
	return commandArguments.size();
}


void SRCommand::addArgument(QString argumentName, QStringList values)
{
	commandArguments.append(new SRCommandNamedArgument(argumentName, values));
}


void SRCommand::addArgument(QStringList values)
{
	commandArguments.append(new SRCommandArgument(values));
}


void SRCommand::addArgument(SRCommandArgumentInterface *arg)
{
	commandArguments.append((SRCommandArgumentInterface*)arg);
}


bool SRCommand::isCommand(QString s)
{
	return !s.contains(SRCommand::commandSeparator);
}


